// pages/openBookcase/onClose/onClose.js
// pages/openBookcase/onOpen/onOpen.js
const request = require('../../../utils/request.js');
//获取应用实例
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    showChongzhi:false,//显示充值按钮
    showZhifu:false,//显示支付按钮,
    isNew: false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
      var that = this;
      setTimeout(function () {
        that.getToken();
      }, 5000)
  },
  onShow: function () {
    var that = this;

  },
  getToken: function () {
    var that = this;
    // 获取本地存储中的token
    wx.getStorage({
      key: 'token',
      success: function (res) {//有该数据
        console.log(res.data)
        that.setData({
          token: res.data
        })
        var tempToken  = res.data;
        request.getNewOrderList({
          'session_id': tempToken
        },
          (res) => {
            console.log(res)
            that.setData({
              curList: res.data.data,
              total_num: parseInt(res.data.data.total_num)
            })


            //判断是否为新用户

            request.getYouHuiList(
              { "session_id": that.data.token },
              (res) => {
                console.log(res);
                //type=1新用户
                if (res.data.code == "1") {
                  that.setData({
                    isNew: true
                  })
                } else {//不是新用户

                  var tempLendNum = parseInt(res.data.data.return_num);//还书数目
                  var tempMoney = parseInt(res.data.data.pay_money);//应支付的金额
                  var tempBalance = parseInt(res.data.data.balance);//当前余额

                  if (tempLendNum > 0) {
                    if (tempMoney <= tempBalance) {//余额充足 直接支付
                      that.setData({
                        showZhifu: false,
                        showChongzhi: false,
                        isNew: false
                      })
                      that.goZhifu();

                    } else if (tempMoney > tempBalance) {//余额不足，直接充值
                      that.setData({
                        showZhifu: false,
                        showChongzhi: true,
                        isNew: false
                      })
                    }
                  } else {
                    that.setData({//直接返回首页
                      showZhifu: false,
                      showChongzhi: false,
                      isNew: false
                    })
                  }



                }

              }
            )



          })
      },
      fail: function (res) {
        console.log(res.data)
        //调用授权方法-设置token
        app.getUserInfo();
      }
    })
  },



  //goFAQ
  goFAQ:function(){
    wx.navigateTo({
      url: '../../my/my-faq/my-faq',
    })
  },
  //分享好友
  shareFriend:function(){
    wx.navigateTo({
      url: "../share/share",
    })
  },
  //返回首页
  goIndex:function(){
    console.log(getCurrentPages())
    wx.navigateBack({
      delta:4
    })
  },
  //去充值
  goChongzhi:function(){
    wx.navigateTo({
      url: '../../my/my-wallet/my-my-wallet',
    })
  },
  //立即支付
  goZhifu:function(){
    var that = this;
    request.reduceManyBalance(
      {
        "session_id": that.data.token
      },
      (res) => {
        console.log(res);
        if (res.data.status == "success") {
          wx.showModal({
            title: '',
            content: '支付成功',
            showCancel: false,
            confirmText: '回首页',
            confirmColor: "#FF0000",
            success: function(res) {
              wx.navigateBack({
                delta:5
              })
            }
          })
        } else if(res.data.status == "error") {
          wx.showToast({
            title: '支付失败',
            image:"../../../images/xx.png"
          })
        }
      }
    )
  }
})